package com.snailthink.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.snailthink.pojo.OrmCustomerVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/json")
public class JsonController {

    @ResponseBody//他不会走视图解析器 会直接返回一个字符串
    @GetMapping("/t1")
    public String t1() {
        OrmCustomerVO customerVO=new OrmCustomerVO();
        customerVO.setCustomerName("AAA");
        customerVO.setCustomerNo("A888");
        customerVO.setCustomerAddress("西安市雁塔区");
        System.out.println(customerVO.toString());
        return "test";
    }

    @ResponseBody//他不会走视图解析器 会直接返回一个字符串
    @GetMapping("/t2")
    public String t2() throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();
        OrmCustomerVO customerVO=new OrmCustomerVO();
        customerVO.setCustomerName("AAA");
        customerVO.setCustomerNo("A888");
        customerVO.setCustomerAddress("西安市雁塔区");
        System.out.println(customerVO.toString());
        String valueAsString = objectMapper.writeValueAsString(customerVO);
        return valueAsString;
    }

    @RequestMapping("/t3")
    @ResponseBody
    public String t3() {
        ObjectMapper objectMappe=new ObjectMapper();
        //默认情况下会将时间转为时间戳，修改默认的json转化配置
        objectMappe.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMappe.setDateFormat(dateFormat);

        String format = dateFormat.format(new Date());

        return format;
    }
}
